package service;


import java.util.List;

import dataDAOImpl.MySQLCommodityBatchDAO;
import dataDAOImpl.MySQLCommodityDAO;
import dataDAOImpl.MySQLOperatorDAO;
import dataDAOImpl.MySQLProductBatchCompDAO;
import dataDAOImpl.MySQLProductBatchDAO;
import dataDAOImpl.MySQLRecipeCompDAO;
import dataDAOImpl.MySQLRecipeDAO;
import dataDAOInterfaces.DALException;
import dataDAOInterfaces.ICommodityBatchDAO;
import dataDAOInterfaces.ICommodityDAO;
import dataDAOInterfaces.IOperatorDAO;
import dataDAOInterfaces.IProductBatchCompDAO;
import dataDAOInterfaces.IProductBatchDAO;
import dataDAOInterfaces.IRecipeCompDAO;
import dataDAOInterfaces.IRecipeDAO;
import dataDTO.CommodityBatchDTO;
import dataDTO.CommodityDTO;
import dataDTO.OperatorDTO;
import dataDTO.ProductBatchCompDTO;
import dataDTO.ProductBatchDTO;
import dataDTO.RecipeCompDTO;
import dataDTO.RecipeDTO;


public class DataService {

	public DataService() {}

	public String getOperators()
	{
		String data="";
		List<OperatorDTO> dList;
		
		IOperatorDAO d = new MySQLOperatorDAO();
		try {
			dList = d.getOperatorList();
			for (int i=0; i<dList.size(); i++)
			{
				String role = null;
				switch (Integer.parseInt(dList.get(i).getRole()))
				{
				case 1: role = "Administrator"; break;
				case 2: role = "Farmaceut"; break;
				case 3: role = "Værkfører"; break;
				case 4: role = "Operatør"; break;
				}
				String status = null;
				switch (Integer.parseInt(dList.get(i).getStatus()))
				{
				case 1: status = "Aktiv"; break;
				case 2: status = "Inaktiv"; break;
				}

				int oprId = dList.get(i).getOprId();
				String textColor = d.getOperator(oprId).getStatus().equals("2") ? "#918D8C" : "#3F3F3F";
				data += "<tr style=\"color:"+ textColor + "\"><td>" + oprId + "</td>";
				data += "<td>" + dList.get(i).getOprName()+"</td>";
				data += "<td>" + dList.get(i).getIni() + "</td>";
				data += "<td>" + dList.get(i).getCpr() + "</td>";
				data += "<td>" + dList.get(i).getPassword() +"</td>";
				data += "<td>" + status +"</td>";
				data += "<td>" + role +"</td>";
				if (dList.get(i).getStatus().equals("1"))
				{
					data += "<td><a class=\"edit\" href=\"Admin?page=3&amp;id="+ oprId+"\">Rediger</a></td>";
					data += "<td><a class=\"delete\" href=\"Admin?page=4&amp;id="+oprId+"\">Deaktiver</a></td></tr>";
				}
				else if (dList.get(i).getStatus().equals("2"))
				{
					data += "<td></td>";
					data += "<td><a class=\"delete\" href=\"Admin?page=5&amp;id="+ oprId+"\">Aktiver</a></td>";
				}
			}
		} catch (DALException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	public String getCommodityBatch()
	{
		String data="";
		List<CommodityBatchDTO> dList;

		ICommodityBatchDAO d = new MySQLCommodityBatchDAO();
		try {
			dList = d.getCommodityBatchList();
			for (int i=0; i<dList.size(); i++)
			{
				int oprId = dList.get(i).getCbId();
				data += "<tr><td style=\"width:20%\">" + oprId + "</td>";
				data += "<td style=\"width:20%\">" +dList.get(i).getCommodityId() + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getVolume() + "</td>";
				data += "<td style=\"width:10%\"><a class=\"edit\" href=\"Vaerft?page=6&amp;id="+ oprId+"\">Rediger</a></td></tr>";
			}
		} catch (DALException e) {
			System.out.println("Fejl: "+e.getLocalizedMessage());
		}
		return data;
	}

	public String getProductBatch()
	{
		String data="";
		List<ProductBatchDTO> dList;

		IProductBatchDAO d = new MySQLProductBatchDAO();
		try {
			dList = d.getProductBatchList();
			for (int i=0; i<dList.size(); i++)
			{
				int oprId = dList.get(i).getPbId();
				data += "<tr><td style=\"width:20%\">" + oprId + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getRecipeId() +"</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getStatus() + "</td>";
				data += "<td style=\"width:10%\"><a class=\"edit\" href=\"Vaerft?page=3&amp;id="+ oprId+"\">Rediger</a></td>";
				data += "<td><a class=\"edit\" href=\"Vaerft?page=7&amp;id="+oprId+"\">Vis</a></td></tr>";

			}
		} catch (DALException e) {
			System.out.println("Fejl: "+e);
		}
		return data;
	}

	public String getCommodities()
	{
		String data = "";
		List<CommodityDTO> dList;

		ICommodityDAO d = new MySQLCommodityDAO();
		try
		{
			dList = d.getCommodityList();
			for (int i=0; i<dList.size(); i++)
			{
				int commodityId = dList.get(i).getCommodityId();
				data += "<tr><td style=\"width:10%\">" + commodityId + "</td>";
				data += "<td style=\"width:30%\">" +dList.get(i).getCommodityName()+"</td>";
				data += "<td style=\"width:30%\">" + dList.get(i).getSupplier() + "</td>";
				data += "<td><a class=\"edit\" href=\"Farma?page=3&amp;id="+ commodityId +"\">Rediger</a></td>";

			}
		}
		catch (DALException e)
		{
			System.out.println("Fejl: "+e);
		}
		return data;
	}
	
	public String getRecipeComp()
	{
		String data = "";
		List<RecipeCompDTO> dList;

		IRecipeCompDAO d = new MySQLRecipeCompDAO();
		try
		{
			dList = d.getRecipeCompList(Integer.parseInt(this.getRecipeId()));
			for (int i=0; i<dList.size(); i++)
			{
				data += "<tr><td style=\"width:20%\">" + dList.get(i).getCommodityId()+ "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getNomNetto() + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getTolorance() + "</td></tr>";

			}
		}
		catch (DALException e)
		{
			System.out.println("Fejl: "+e);
		}
		return data;
	}

	static String areaAccess = "";
	public String getAreaaccess() { return areaAccess; }

	public void setAreaaccess(String role)
	{
		if (role.equals("1"))
		{
			areaAccess = "<br><div style=\"color:#3F3F3F;text-align:right\">Skift område<br><a class=\"edit\" href=\"Admin\">Admin</a> | <a class=\"edit\" href=\"Farma\">Farmaceut</a> | <a class=\"edit\" href=\"Vaerft\">Værkfører</a><br><a class=\"delete\" href=\"Afvejningssystem?logout=true\">Log ud</a></div>";
		} 
		else if (role.equals("2"))
		{
			areaAccess = "<br><div style=\"color:#3F3F3F;text-align:right\">Skift område<br><a class=\"edit\" href=\"Farma\">Farmaceut</a> | <a class=\"edit\" href=\"Vaerft\">Værkfører</a><br><a class=\"delete\" href=\"Afvejningssystem?logout=true\">Log ud</a></div>";
		}
		else
		{
			areaAccess = "<br><br><br><div style=\"text-align:right\"><a class=\"delete\" href=\"Afvejningssystem?logout=true\">Log ud</a></div>";
		}

	}

	public String getRecipes()
	{
		String data = "";
		List<RecipeDTO> recipeList;

		IRecipeDAO recipe = new MySQLRecipeDAO();

		try
		{
			recipeList = recipe.getRecipeList();  //Henter Recept listen
			for (int i=0; i<recipeList.size(); i++)
			{
				data += "<tr><td style=\"width:20%\">" + recipeList.get(i).getRecipeId() + "</td>";
				data += "<td style=\"width:20%\">" +recipeList.get(i).getRecipeName()+"</td>";
				data += "<td><a class=\"edit\" href=\"Farma?page=6&amp;id="+ recipeList.get(i).getRecipeId() +"\">Vis</a></td>";
				data += "<td><a class=\"edit\" href=\"Farma?page=7&amp;id="+ recipeList.get(i).getRecipeId() +"\">Tilføj råvare</a></td></tr>";
			}
		}
		catch (DALException e) 	{ System.out.println("Fejl: "+e); }
		return data;
	}
	
	public String getProductBatchComp()
	{
		String data = "";
		List<ProductBatchCompDTO> dList;

		IProductBatchCompDAO d = new MySQLProductBatchCompDAO();
		try
		{
			dList = d.getProductBatchCompList(Integer.parseInt(this.getPbId()));
			for (int i=0; i<dList.size(); i++)
			{
				data += "<tr><td style=\"width:20%\">" + dList.get(i).getOprId() + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getCbId() + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getTara() + "</td>";
				data += "<td style=\"width:20%\">" + dList.get(i).getNetto() + "</td></tr>";
			}
		}
		catch (DALException e)
		{
			System.out.println("Fejl: "+e);
		}
		return data;
	}
	
	public String getCommodityNames()
	{
		String data = "";
		List<CommodityDTO> dList;

		ICommodityDAO d = new MySQLCommodityDAO();
		try
		{
			dList = d.getCommodityList();
			for (int i=0; i<dList.size(); i++)
			{
				int commodityId = dList.get(i).getCommodityId();
				data += "<option>" + commodityId + " - " + dList.get(i).getCommodityName() + "</option>";

			}
		}
		catch (DALException e)
		{
			System.out.println("Fejl: "+e);
		}
		return data;
	}
	
	public String getRecipeNames()
	{
		String data = "";
		List<RecipeDTO> recipeList;
		
		IRecipeDAO recipe = new MySQLRecipeDAO();
		try
		{
			recipeList = recipe.getRecipeList();  //Henter Recept listendList = d.getCommodityList();
			for (int i=0; i<recipeList.size(); i++)
			{
				int recipeId = recipeList.get(i).getRecipeId();
				data += "<option>" + recipeId + " - " + recipeList.get(i).getRecipeName() + "</option>";

			}
		}
		catch (DALException e)
		{
			System.out.println("Fejl: "+e);
		}
		return data;
	}

	String rbId="", volume="";
	public String getRbId() {return rbId;}
	public void setRbId(String rbId) {this.rbId = rbId;}
	public String getVolume() {return volume;}
	public void setVolume(String volume) {this.volume = volume;}

	String pbId="", recipeId="", status="";	
	public String getPbId() 					{return pbId;}
	public void setPbId(String pbId) 			{this.pbId = pbId;}
	public String getRecipeId() 				{return recipeId;}
	public void setRecipeId(String recipeId)	{this.recipeId = recipeId;}
	public String getStatus() 					{return status;}
	public void setStatus(String status) 		{this.status = status;}

	String name = "", id = "", ini = "", cpr = "", pass = "";
	public String getId()				{ return id; }
	public String getName()				{ return name; }
	public String getCpr()				{ return cpr; }
	public String getIni()				{ return ini; }
	public String getPass()				{ return pass; }
	public void setId(String id)		{ this.id = id; }
	public void setName(String name)	{ this.name = name; }
	public void setIni(String ini)		{ this.ini = ini; }
	public void setCpr(String cpr)		{ this.cpr = cpr; }
	public void setPass(String pass)	{ this.pass = pass; }

	String commodityNr = "", commodityName = "", commoditySupplier = "";
	public String getCommoditynr()								{ return commodityNr; }
	public String getCommodityname()							{ return commodityName; }
	public String getCommoditysupplier()						{ return commoditySupplier; }
	public void setCommodityNr(String commodityNr)				{ this.commodityNr = commodityNr; }
	public void setCommodityName(String commodityName)			{ this.commodityName = commodityName; }
	public void setCommoditySupplier(String commoditySupplier)	{ this.commoditySupplier = commoditySupplier; }
}